package com.lovo.bbs.listener;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

import com.lovo.bbs.bo.BoardBo;
import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.LinkBo;
import com.lovo.bbs.bo.RankBo;
import com.lovo.bbs.vo.BoardVo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;
import com.lovo.bbs.vo.LinkVo;
import com.lovo.bbs.vo.LoginVo;
import com.lovo.bbs.vo.RankVo;

/**
 * 初始化ServletContext数据
 * 在服务器启动或项目部署时自动运行
 * @author tiancen2001
 * 
 */
public class InitGlobalListen extends HttpServlet implements
		ServletContextListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2984446878010839631L;

	public void contextDestroyed(ServletContextEvent arg0) {
	}

	public void contextInitialized(ServletContextEvent sce) {
		ArrayList<BoardVo> boardList = null;//板块列表
		boardList = new BoardBo().getAllBoard();
		
		ArrayList<ForumVo> forumList = null;//论坛列表
		ForumBo forumBo = new ForumBo();
		forumList = forumBo.getAllForum();
		
		HashMap<Integer, ForumVo> forumMap = null;//论坛HashMap
		forumMap = forumBo.getForumMap(forumList);
		
		HashMap<Integer,RankVo> rankMap = null;//等级信息HashMap
		rankMap = new RankBo().getRankMap();
		
		ForumStatBo forumStatBo = new ForumStatBo();
		ForumStatVo forumInfo = forumStatBo.getForumInfo();//网站杂项信息
		
		ArrayList<LinkVo> linkList = null;
		linkList = new LinkBo().getAllLink();
		
		ServletContext context = sce.getServletContext();
		
		context.setAttribute("boardList", boardList);// 板块列表
		context.setAttribute("forumList", forumList);// 论坛列表
		context.setAttribute("forumMap", forumMap);// 论坛Map
		context.setAttribute("rankMap", rankMap);// 等级Map
		context.setAttribute("forumInfo", forumInfo);// 网站杂项信息
		context.setAttribute("linkList", linkList);// 链接
		
		//在线用户列表,初始为空
		HashMap<String,LoginVo> onlineUser = new HashMap<String,LoginVo>();
		context.setAttribute("onlineUser", onlineUser);
		
		//数据库初始化
		forumStatBo.calcTodayTopicNum();//计算网站今日主题数
		forumBo.calcTodayTopicNum();//分别计算各个论坛的今日主题数
		}

}
